﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;

namespace IRM.Messaging
{
    public enum Action
    {
        None,
        Insert,
        Update,
        Delete
    }

    [Table(Name = "dbo.irm_ApplicationQueue")]
    public class Message
    {
        int id = -1;
        string entity;
        int entityId;
        Action action;
        string data;
        string error;
        QueueStatus status;
        string topic;
        DateTime? rescheduleUntil;
        string createdBy;
        DateTime created = DateTime.Now;
        DateTime changed;

        public Message()
        {
            if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
                createdBy = System.Threading.Thread.CurrentPrincipal.Identity.Name;
            changed = created;
        }

        [Column(Storage = "id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
        public int Id { get { return this.id; } }

        [Column(Storage = "entity", DbType = "VarChar(2000) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public string Entity 
        {
            get { return this.entity; }
            set
            {
                if (this.entity != value)
                {
                    this.entity = value;
                    if (string.IsNullOrEmpty(this.topic))
                        this.Topic = this.entity;
                    OnPropertyChanged("Entity");
                }
            }
        }

        [Column(Storage = "entityId", DbType = "Int NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public int EntityId 
        {
            get { return this.entityId; }
            set
            {
                if (this.entityId != value)
                {
                    this.entityId = value;
                    OnPropertyChanged("EntityId");
                }
            }
        }

        [Column(Storage = "action", DbType = "Int NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public Action Action
        {
            get { return this.action; }
            set
            {
                if (this.action != value)
                {
                    this.action = value;
                    OnPropertyChanged("Action");
                }
            }
        }
        
        [Column(Storage = "data", DbType = "VarChar(MAX)", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
        public string Data 
        {
            get { return this.data; }
            set
            {
                if (this.data != value)
                {
                    this.data = value;
                    OnPropertyChanged("Data");
                }
            }
        }

        [Column(Storage = "status", Name = "QueueStatusId", DbType = "Int NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public QueueStatus Status 
        {
            get { return this.status; }
            internal set
            {
                if (this.status != value)
                {
                    this.status = value;
                    OnPropertyChanged("Status");
                }
            }
        }

        [Column(Storage = "rescheduleUntil", CanBeNull=true, DbType = "smalldatetime", UpdateCheck = UpdateCheck.Never)]
        public DateTime? RescheduleUntil
        {
            get { return this.rescheduleUntil; }
            set
            {
                if (this.rescheduleUntil != value)
                {
                    this.rescheduleUntil = value;
                    OnPropertyChanged("RescheduleUntil");
                }
            }
        }

        [Column(Storage = "topic", DbType = "VarChar(2000) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public string Topic 
        {
            get { return this.topic; }
            set
            {
                if (this.topic != value)
                {
                    this.topic = value;
                    OnPropertyChanged("Topic");
                }
            }
        }

        [Column(Storage = "error", DbType = "VarChar(MAX)", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
        public string Error
        {
            get { return this.error; }
            set
            {
                if (this.error != value)
                {
                    this.error = value;
                    OnPropertyChanged("Error");
                }
            }
        }

        [Column(Storage = "createdBy", DbType = "VarChar(200) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public string CreatedBy 
        {
            get { return this.createdBy; }
        }

        [Column(Storage = "created", DbType = "smalldatetime NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public DateTime Created
        {
            get { return this.created; }
        }

        [Column(Storage = "changed", DbType = "smalldatetime NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public DateTime Changed 
        {
            get { return this.changed; }
        }

        public bool IsNew()
        {
            return this.id < 1;
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.IsNew() == false)
                this.changed = DateTime.Now;
        }
    }
}
